<?php
 if(!isset($GLOBALS['config']['to_install']))
    require_once("DB.php");
    
 class __driver_db {
 	
 	
    
  // Hold an instance of the class
    private static $instance, $host, $dbname, $login, $pass;

    /**
     * A private constructor; prevents direct creation of object
     *
     * @param <array> $params : login, pass, host, dbname
     */
    private function __construct($params = array())
    {

    }

    // The singleton method
    public static function si($params = array())
    {
        if(isset($GLOBALS['config']['to_install'])) return false;
        
        if (!isset(self::$instance) && !isset(self::$login) && !isset(self::$pass) && !isset(self::$host) && !isset(self::$dbname)) {
            self::$dbname = $params['dbname'];
            self::$host   = $params['host'];
            self::$login  = $params['login'];
            self::$pass   = $params['pass'];

            if(isset(self::$login) && isset(self::$pass) && isset(self::$host) && isset(self::$dbname)) {
                self::$instance = &DB::connect("mysql://".self::$login.':'.self::$pass."@".self::$host."/".self::$dbname, array('debug'=>2, 'portability'=>DB_PORTABILITY_ALL));

                if (PEAR::isError(self::$instance)){ 
                    if($params['return_fail'] == true) return false;
                    else echo"<b>ERROR! Failed connect to DB! Code - 2</b> "; exit();
                }

                self::$instance->query('SET CHARACTER SET utf8');
                self::$instance->query('SET NAMES utf8');
                self::$instance->autoCommit(false);
            } else {
                if($params['return_fail'] == true) return false;
                else exit("<b>ERROR!</b> Failed connect to DB! Code - 1");
            }
        }

        return self::$instance;
    }

    // Prevent users to clone the instance
    public function __clone()
    {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
 
 }

?>
